package com.jiepos.mpos.biz.outservice.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.ServiceInterMchntDao;
import com.jiepos.mpos.common.entity.App_Ins_InfEntity;
import com.jiepos.mpos.common.entity.App_Service_Inter_MchntEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 *
 *
 * <p>标题: —— 业务系统一机一码类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos]
 * @Package com.jiepos.mpos.biz.appInfM.service
 * @author 余翼膑
 * @version 1.0
 * @since 2017年9月22号
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class ServiceInterMchntService extends BaseService {

    @Autowired
    private ServiceInterMchntDao serviceInterMchntDao;

    public Map getServicemchntList(HttpServletRequest request, Pagination pagination, App_Service_Inter_MchntEntity appServiceInterMchntEntity,App_Ins_InfEntity infEntity) {
        StringBuffer sqlBuf = getInitSql("t1").append(",");
        sqlBuf.append(" t2.ins_cd || '-' || t2.ins_cn_nm as inst_name, ")
                .append("  t1.mchnt_cd, ")
                .append("  t1.term_id, ")
                .append("  t1.mchnt_nm, ")
                .append("  t1.service_mchnt_cd, ")
                .append("  t1.service_term_id ")
                .append("   FROM "+ DBConstant.Table.T_APP_SERVICE_INTER_MCHNT+" t1 ")
                .append("  left join "+ DBConstant.Table.T_APP_INS_INF+" t2 ")
                .append("  on t1.ins_uuid = t2.ins_uuid  where 1=1 ");

        List list = new ArrayList();

        //机构号
        if (!StringUtils.isBlank(infEntity.getIns_cd())) {
            sqlBuf.append( " and t2.ins_cd like ? " );
            list.add("%" + infEntity.getIns_cd() + "%");
        }
        //机构名称
        if (!StringUtils.isBlank(infEntity.getIns_cn_nm())) {
            sqlBuf.append( " and t2.ins_cn_nm like ? " );
            list.add("%" + infEntity.getIns_cn_nm() + "%");
        }
        //会员编号
        if (!StringUtils.isBlank(appServiceInterMchntEntity.getMchnt_cd())) {
            sqlBuf.append( " and t1.mchnt_cd like ? " );
            list.add("%" + appServiceInterMchntEntity.getMchnt_cd() + "%");
        }
        //会员名称
        if (!StringUtils.isBlank(appServiceInterMchntEntity.getMchnt_nm())) {
            sqlBuf.append( " and t1.Mchnt_nm like ? " );
            list.add("%" + appServiceInterMchntEntity.getMchnt_nm() + "%");
        }
        //业务系统商户号
        if (!StringUtils.isBlank(appServiceInterMchntEntity.getService_mchnt_cd())) {
            sqlBuf.append( " and t1.Service_mchnt_cd like ? " );
            list.add("%" + appServiceInterMchntEntity.getService_mchnt_cd() + "%");
        }
        pagination.queryString = sqlBuf.toString();
        pagination = getInsAuthSql(request, pagination, "t1", null);
        pagination = this.getOrderSql(pagination, "t1");
        return serviceInterMchntDao.getPaginationMap(pagination,"t1",list);

    }

	/**
	 * 通过渠道方商户号查询关联关系
	 * @param serviceMchntCd 外部商户号
	 * @return
	 */
	public App_Service_Inter_MchntEntity findByServiceMchntCd(String serviceMchntCd){
		return serviceInterMchntDao.findByParam("service_mchnt_cd", serviceMchntCd);
	}
	
	/**
	 * 通过商户号查询关联关系
	 * @param mchntCd 商户编号
	 * @return
	 */
	public App_Service_Inter_MchntEntity findByMchntCd(String mchntCd){
		return serviceInterMchntDao.findByParam("mchnt_cd", mchntCd);
	}
	
	/**
	 * 保存实体
	 * @param entity
	 */
	public App_Service_Inter_MchntEntity save(App_Service_Inter_MchntEntity entity){
		if(entity == null){
			return null;
		}
		if(StringUtils.isBlank(entity.getService_inter_mchnt_uuid())){
			entity.setService_inter_mchnt_uuid(IDFactoryUtil.getUUID());
			entity.setRec_st(SystemConstant.NORMAL_STATUS);
			entity.setLast_oper_in(SystemConstant.OPER_ADD);
			entity.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
			entity.setRec_upd_ts(entity.getRec_crt_ts());
			serviceInterMchntDao.save(entity);
		}else{
			entity.setLast_oper_in(SystemConstant.OPER_UPDATE);
			entity.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			serviceInterMchntDao.update(entity, null);
		}
		return entity;
	}
}
